package com.ict.luxshare.jobservice;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

public class PopupActivity extends AppCompatActivity {
    private static final String TAG="jb:PopupActivity";
    private static ThinBTClient mThinBTClient;
    public static final int UPDATE_TITLE = 0;
    public static final int CHECK_BATTERY = 1;
    private static TextView viewBattery = null;
    private static ImageView viewBatteryIcon = null;
    private static boolean isExistNow = false;
    private static boolean mGotBatteryAlready = false;

    private static Handler mainHandler= new Handler()
    {
        public void dispatchMessage(android.os.Message msg) {
            switch (msg.what){
                case UPDATE_TITLE:
                    int nBattery = msg.getData().getInt("Result");
                    viewBattery.setText("" + nBattery + "%");
                    if (nBattery >= 100)
                        viewBatteryIcon.setImageResource(R.drawable.notification_battery_full);
                    else if (nBattery >= 90)
                        viewBatteryIcon.setImageResource(R.drawable.notification_battery_90_comsuming);
                    else if (nBattery >= 80)
                        viewBatteryIcon.setImageResource(R.drawable.notification_battery_80_comsuming);
                    else if (nBattery >= 60)
                        viewBatteryIcon.setImageResource(R.drawable.notification_battery_60_comsuming);
                    else if (nBattery >= 50)
                        viewBatteryIcon.setImageResource(R.drawable.notification_battery_50_comsuming);
                    else if (nBattery >= 30)
                        viewBatteryIcon.setImageResource(R.drawable.notification_battery_30_comsuming);
                    else if (nBattery >= 20)
                        viewBatteryIcon.setImageResource(R.drawable.notification_battery_20_comsuming);
                    else
                        viewBatteryIcon.setImageResource(R.drawable.notification_battery_alert);
                    mThinBTClient.onPause();
                    mGotBatteryAlready = true;
                    break;
                case CHECK_BATTERY:
                    if(isExistNow){
                        Log.d(TAG, "action:CHECK_BATTERY");
                        mThinBTClient.onPause();
                        mThinBTClient.onResume();
                        Message newMsg = mainHandler.obtainMessage();
                        newMsg.what = PopupActivity.CHECK_BATTERY;
                        if(mGotBatteryAlready){
                            mainHandler.sendMessageDelayed(newMsg, 20000);
                        }else {
                            mainHandler.sendMessageDelayed(newMsg, 1000);
                        }
                    }
                    break;
                default:
                    Log.d(TAG, "Other action:" + msg.what);
                    break;
            }
        };
    };

    private void initial() {
        Button btn_Yes = (Button) findViewById(R.id.notification_button);
        btn_Yes.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Log.d(TAG, "Butten Yes");
                        Log.i(TAG, "test");
                        finish();
                    }
                }
        );
        viewBattery = (TextView)findViewById(R.id.earphone_batt_txt);
        viewBatteryIcon = (ImageView)findViewById(R.id.earphone_batt_image);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
            Log.v(TAG, "onCreate()");
            super.onCreate(savedInstanceState);
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            setContentView(R.layout.activity_popup);
            this.getWindow().setGravity(Gravity.BOTTOM);
            this.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
            initial();
            mGotBatteryAlready = false;
            Intent mTempIntent = getIntent();
            String mTempDeviceName = mTempIntent.getStringExtra(BtReceiver.DEVICE_NAME);
            mThinBTClient = new ThinBTClient(this, mainHandler, mTempDeviceName);
            mThinBTClient.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mThinBTClient.onResume();

        isExistNow = true;
        Message newMsg = mainHandler.obtainMessage();
        newMsg.what = PopupActivity.CHECK_BATTERY;
        mainHandler.sendMessageDelayed(newMsg, 5000);
    }


    @Override
    protected void onPause() {
        isExistNow = false;
        super.onPause();
        mThinBTClient.onPause();
        mGotBatteryAlready = false;
    }

}
